package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.CourseTeacher;
import com.hznu.jspFinal.myBeans.CourseTeacherSupport;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.CourseTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(description = "教学班管理接口",value = "courseTeacher")
@RestController
@RequestMapping("/CourseTeacher")
public class CourseTeacherController {
    @Autowired
    private CourseTeacherService courseTeacherService;

    /**
     * author:wp
     * time:16:04 2018/6/3
     *
     * @param courseTeacher
     * @return
     */
    @ApiOperation(value = "添加一个教学班")
    @PostMapping("/add")
    public R addOne(@RequestBody CourseTeacher courseTeacher)
    {
        try{
            courseTeacherService.addOne(courseTeacher);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * time:17:13 2018/6/13
     */
    @ApiOperation(value = "删除教学班")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id)
    {
        try{
            courseTeacherService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "更新教学班信息")
    @PostMapping("/update")
    public R update(@RequestBody CourseTeacher courseTeacher)
    {
        try{
            courseTeacherService.update(courseTeacher);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "通过id查找教学班")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",courseTeacherService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "通过教师id查找他的所有教学班")
    @GetMapping("/findByTeacherid/{id}")
    public R findByTeacherid(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",courseTeacherService.findByTeacherid(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("通过教师id和课程id找到教学班级")
    @PostMapping("/findByCourseidAndTeachId")
    public R findByCourseidAndTeachId(@RequestBody CourseTeacherSupport courseTeacherSupport){
        try{
            return R.ok().put("data",courseTeacherService.selectByCourseIdAndTeachId(courseTeacherSupport.getCourseid(),courseTeacherSupport.getTeacherid()));
        }
        catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
